<?php
/**
 * Created by PhpStorm.
 * User: 陈艺赫
 * Date: 2018/5/4
 * Time: 11:18
 */

namespace app\user\controller;

use app\user\model\Functions;
use app\user\model\User;
use think\Controller;

class Set extends Controller
{
    /**
     * 获取程序所有controller文件名
     */
    public static function getControllerNameList($modules)
    {
        $controllerNameLists = [];
        foreach ($modules as $module) {
            $controllerDir = APP_PATH . $module . '/controller';
            $fileNameList = scandir($controllerDir);

            $controllerNameList = [];
            foreach ($fileNameList as $key => $fileName) {
                if (strpos($fileName, '.php')) {
                    $controllerNameList[] = str_replace('.php', '', $fileName);
                }
            }
            $controllerNameLists[$module] = $controllerNameList;
        }
        return $controllerNameLists;
    }

    /**
     * @param $controller
     * 获取controller下action
     * @return array
     */
    public static function getFunctionListByController($module, $controller)
    {
        $action = [];
        $controllerName = sprintf('app\\' . $module . '\\controller\\%s', ucfirst($controller));
        $ReflectionClass = new \ReflectionClass($controllerName);

        foreach ($ReflectionClass->getMethods(\ReflectionMethod::IS_PUBLIC) as $rM) {
            if (strpos($rM->getName(), '_') !== 0) { //排除魔术方法
                $func['module'] = strtolower($module);
                $func['controller'] = strtolower($controller);
                $func['function'] = strtolower($rM->getName());
                $func['identify'] = $func['module'] . '/' . $func['controller'] . '/' . $func['function'];
                $action[] = $func;
            }
        }
        return $action;
    }

    /*
     * 更新所有权限方法
     * */
    public function refreshAllFunctions()
    {
        $m_list = config('module');
        $c_list = self::getControllerNameList($m_list);
//        dump($c_list);
        $func_data = [];
        foreach ($c_list as $module => $f_list) {
            foreach ($f_list as $controller) {
                $action = self::getFunctionListByController($module, $controller);
                if (!empty($action)) {
                    $func_data = array_merge($func_data, $action);
                }

            }
        }
//        dump($func_data);
        $db_function = new Functions;

        $temp_func = [];
        foreach ($func_data as $func) {
            $func['module'] = strtolower($func['module']);
            $func['controller'] = strtolower($func['controller']);
            $func['function'] = strtolower($func['function']);
            $temp_func[] = $func['module'] . '/' . $func['controller'] . '/' . $func['function'];
            $data_function = $db_function
                ->where('module', $func['module'])
                ->where('controller', $func['controller'])
                ->where('function', $func['function'])
                ->find();
            if (empty($data_function)) {
                $db_function->insert($func);
            } else {
                $function_list = $data_function->toArray();
                $function_list = update_all_column($function_list, $func);
                $db_function->update($function_list);
            }
        }

        //删除无用Function
        if (!empty($temp_func)) {
            $delete_list = $db_function->where('identify', 'not in', $temp_func)->column('id');
            $db_function->where('id', 'in', $delete_list)->delete();
        }

        $this->success('重置成功!');
    }

    public function test()
    {
        $user = new User();
        $r = $user->with('parent')->select()->toArray();
        dump($r);
    }

    public function test2()
    {
        $r = User::makePasswordAndSalt('123456');
        dump($r);
    }


}